<?php

/**
 * Created by PhpStorm.
 * User: Evan
 * Date: 16/3/18
 * Time: 上午10:10
 */
class Public_special extends MY_Agent_Controller
{
    public function index()
    {
        $limit   = 200;
        $curPage = (int)$this->input->get_post('per_page');
        $offset  = ($curPage < 1) ? 0 : ($curPage - 1) * $limit;


        $name   = trim($this->input->get_post('name'));
        $cid    = (int)$this->input->get_post('cid');
        $status = $this->input->get_post('status');
        $time   = $this->input->get_post('create_time');
        $sid    = (int)$this->input->get_post('sid');


        $where = array();
        $name && $where['name like \'%'.$name.'%\''] = null;
        $cid  && $where['cid'] = $cid;
        ($status || $status == '0')   && $where['status'] = $status;
        if ($time)
        {
            $eTime = explode('-', $time);
            $where['create_time >='] = date('Y-m-d H:i:s', strtotime($eTime[0]));
            $where['create_time <='] = date('Y-m-d ', strtotime($eTime[1])).'23:59:59';
        }


        $this->load->model('Model_public_special', 'special');
        $this->load->model('Model_public_cate', 'pc');

        $totalNum = $this->special->count();
        $info = $this->special->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('Public_cate_feature/index', $totalNum, $limit);
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $cate = $this->pc->lists(0, 1000);
        $special_data = $this->special->lists(0, 1000);

        $data = array(
            'title'    => '公众号专题列表',
            'info'     => $info,
            'cate'     => sortData($cate),
            'pageHtml' => $pageHtml,
            'totalNum' => $totalNum,
            'status'   => $status,
            'cid'      => $cid,
            'name'     => $name,
            'time'     => $time,
            'special_data' => $special_data,
        );

        $this->load->view('agent/public/special_index', $data);
    }



    public function detail()
    {
        $sid     = (int)$this->uri->segment(3);
        $limit   = 200;
        $curPage = (int)$this->input->get_post('per_page');
        $offset  = ($curPage < 1) ? 0 : ($curPage - 1) * $limit;


        $this->load->model('Model_public_special', 'special');
        $item = $this->special->getById($sid);
        if (empty($item))
            show_error('公众号专题不存在!');


        $where = array(
            'sid' => $sid,
        );
        $this->load->model('Model_public_special_relation', 'sr');

        $totalNum = $this->sr->count();
        $info = $this->sr->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('Public_cate_feature/detail', $totalNum, $limit);
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $list = array();
        if (!empty($info))
        {
            $pids = array_column($info, 'pid');
            $this->load->model('Model_public', 'public');
            $list = $this->public->getByIn('pid', $pids);
        }



        $this->load->model('Model_public_cate', 'pc');
        $cate = $this->pc->lists(0, 1000);

        $data = array(
            'title'    => $item['name'].'公众号专题详情',
            'info'     => $info,
            'list'     => $list,
            'cate'     => sortData($cate),
            'pageHtml' => $pageHtml,
            'totalNum' => $totalNum,
            'items'    => $item,

        );

        $this->load->view('agent/public/special_detail', $data);
    }


    public function add_public_to_special()
    {
        $response = array('code' => '0', 'msg' => '添加成功');

        do {
            $pid = (int)$this->input->get_post('pk');
            $sid = (int)$this->input->get_post('value');

            if (!$pid || !$sid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_public', 'public');
            $item = $this->public->getById($pid);

            if (empty ($item))
            {
                $response = error(300003);
                break;
            }


            $this->load->model('Model_public_special', 'special');
            $item = $this->special->getById($sid);
            if (empty($item))
            {
                $response = error(300015);
                break;
            }


            $this->load->model('Model_public_special_relation', 'sr');
            $res = $this->sr->save(array('pid' => $pid, 'sid' => $sid));
            if (!$res)
            {
                $response = error(300019);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    /**
     * 删除专题中的公众号
     */
    public function delete_special_public()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $rid = (int)$this->input->get_post('rid');

            if (!$rid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_public_special', 'special');
            $this->load->model('Model_public_special_relation', 'sr');
            $item = $this->sr->getById($rid);

            if (empty ($item))
            {
                $response = error(300017);
                break;
            }


            $res = $this->sr->delete(array('rid' => $rid));
            if (!$res)
            {
                $response = error(300018);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    public function create()
    {
        $sid = (int)$this->uri->segment(3);

        $this->load->model('Model_public_cate', 'pc');
        $cate = $this->pc->lists(0, 10000);


        $item = array();
        if ($sid)
        {
            $this->load->model('Model_public_special', 'special');
            $item = $this->special->getById($sid);
        }


        $data = array(
            'title' => '编辑公众号专题',
            'data'  => $item,
            'cate'  => sortData($cate),
        );

        $this->load->view('agent/public/special_create', $data);
    }


    public function save()
    {
        $sid = (int)$this->input->get_post('sid');
        $pic = (int)$this->input->get_post('pic');


        $data = array(
            'name'    => trim($this->input->get_post('name')),
            'cid'     => (int)$this->input->get_post('cid'),
            'sort'    => (int)$this->input->get_post('sort'),
            'title'   => trim($this->input->get_post('title')),
            'keyword' => trim($this->input->get_post('keyword')),
            'descr'   => trim($this->input->get_post('descr')),
            'summary' => trim($this->input->get_post('summary')),
        );


        empty($data['title'])   && $data['title'] = $data['name'].'类微信公众号专题集合';
        empty($data['keyword']) && $data['keyword'] = $data['name'].'公众号集合，'.$data['name'].'公众号专题，'.$data['name'].'应用号集合';
        empty($data['descr'])   && $data['descr'] = '公众号12网精选'.$data['name'].'类公众号，打造'.$data['name'].'公众号专题，为您提供最新最热门的'.$data['name'].'类公众号信息。';


        $pic && $data['pic'] = $pic;


        if (empty ($data['name']))
            show_error('公众号专题名称为空!');


        $this->load->model('Model_public_special', 'special');
        $rs = $this->special->save($data, $sid ? $sid : null);

        if (!$rs)
            show_error('保存公众号专题失败!');


        $this->load->helper('url');
        redirect(url('admin').'Public_special/index');
    }


    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $sid = (int)$this->input->get_post('sid');

            if (!$sid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_public_special', 'special');
            $item = $this->special->getById($sid);

            if (empty ($item))
            {
                $response = error(300015);
                break;
            }



            $res = $this->special->delete(array('sid' => $sid));
            if (!$res)
            {
                $response = error(300016);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}